﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace Tracy.CoreLib.Data
{
    public class TracyDataType : XmlModel_BasicFunctions, IXmlModelItem
    {
        #region 构造函数
        public TracyDataType() : base() { }
        public TracyDataType(string xml) : base(xml) { }
        public TracyDataType(XmlNode node) : base(node) { }
        #endregion

        private string _name = "";

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }    

        private string _subType = "";

        public string SubType
        {
            get { return _subType; }
            set { _subType = value; }
        }

        #region IXmlModel Members

        public string XmlNodeName
        {
            get { return Constants.CONST_TAG_TYPE; }
        }

        public string GetValue(string key)
        {
            if (key == Constants.CONST_TAG_NAME) return Name;
            if (key == Constants.CONST_TAG_SUBTYPE) return SubType;
            return "";
        }

        public void SetValue(string key, string value)
        {
            if (key == Constants.CONST_TAG_NAME) Name = value;
            if (key == Constants.CONST_TAG_SUBTYPE) SubType = value;
        }

        #endregion

        #region IKeyedItem Members

        public string Key
        {
            get { return Name; }
        }

        #endregion

        protected override void ParseXml(XmlNode node)
        {
            Name = node.InnerText;
            XmlNode subNode = SelectAttribute(node, Constants.CONST_TAG_SUBTYPE, false);
            if (subNode != null) SubType = subNode.InnerText;
        }

        protected override void GenerateXml(XmlDocument doc, XmlNode newNode)
        {
            newNode.InnerText = Name;
            if (SubType != "") AppendAttribute(doc, newNode, Constants.CONST_TAG_SUBTYPE, SubType);
        }
    }
    public class TracyProperty : ManagedXmlModel_CommonFunctions, IManagedXmlModelItem
    {

        #region 构造函数
        public TracyProperty() : base() { }
        public TracyProperty(string xml) : base(xml) { }
        public TracyProperty(XmlNode node) : base(node) { }
        #endregion

        #region IXmlModel Members

        public string XmlNodeName
        {
            get { return Constants.CONST_TAG_ITEM; }
        }

        #endregion

        #region IKeyedItem Members

        public string Key
        {
            get { return Name; }
        }

        #endregion

        public string Name
        {
            get
            {
                if (Data.ContainsKey(Constants.CONST_TAG_NAME)) return Data[Constants.CONST_TAG_NAME];
                else return "";
            }
            set
            {
                Data[Constants.CONST_TAG_NAME] = value;
            }
        }

        public string Description
        {
            get
            {
                if (Data.ContainsKey(Constants.CONST_TAG_DESCRIPTION)) return Data[Constants.CONST_TAG_DESCRIPTION];
                else return "";
            }
            set
            {
                Data[Constants.CONST_TAG_DESCRIPTION] = value;
            }
        }

        private IXmlModelCollection _sourceTypes;
        /// <summary>
        /// 源类型
        /// </summary>
        public IXmlModelCollection SourceTypes
        {
            get { if (_sourceTypes == null)_sourceTypes = new XmlModelCollection(); return _sourceTypes; }
        }

        private IXmlModelCollection _targetTypes;
        /// <summary>
        /// 目标类型
        /// </summary>
        public IXmlModelCollection TargetTypes
        {
            get { if (_targetTypes == null)_targetTypes = new XmlModelCollection(); return _targetTypes; }
        }

        protected override void PreParseXml(XmlNode node)
        {
            Name = SelectNode(node, Constants.CONST_TAG_NAME, true).InnerText;
            XmlNode typeNode = SelectNode(node, Constants.CONST_TAG_SOURCETYPES);
            if (typeNode != null)
            {
                foreach (XmlNode n in typeNode.SelectNodes(Constants.CONST_TAG_TYPE))
                {
                    SourceTypes.Add(new TracyDataType(n));
                }
            }
            typeNode = SelectNode(node, Constants.CONST_TAG_TARGETTYPES);
            if (typeNode != null)
            {
                foreach (XmlNode n in typeNode.SelectNodes(Constants.CONST_TAG_TYPE))
                {
                    TargetTypes.Add(new TracyDataType(n));
                }
            }
        }

        protected override void PostGenerateXml(XmlDocument doc, XmlNode node)
        {
            XmlNode typeNode = doc.CreateElement(Constants.CONST_TAG_SOURCETYPES);
            foreach (TracyDataType type in SourceTypes)
            {
                typeNode.AppendChild(type.ToXmlNode(doc));
            }
            node.AppendChild(typeNode);

            typeNode = doc.CreateElement(Constants.CONST_TAG_TARGETTYPES);
            foreach (TracyDataType type in TargetTypes)
            {
                typeNode.AppendChild(type.ToXmlNode(doc));
            }
            node.AppendChild(typeNode);
        }
    }

    public class TracyPropertyProvider : XmlModelProvider_CommonFunctions, IXmlModelProvider
    {
        #region 构造函数
        public TracyPropertyProvider() : base() { }
        public TracyPropertyProvider(string xml) : base(xml) { }
        public TracyPropertyProvider(XmlNode node) : base(node) { }
        #endregion
        #region IXmlModelContainer Members


        public IXmlModelItem GenerateChild(XmlNode node)
        {
            return new TracyProperty(node);
        }

        #endregion

        #region IXmlModel Members

        public string XmlNodeName
        {
            get { return Constants.CONST_TAG_PROPERTIES; }
        }

        #endregion
    }
}
